package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Depot;
import cn.wolfcode.wms.mapper.DepotMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.DepotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepotServiceImpl implements DepotService {
    @Autowired
    private DepotMapper depotMapper;

    public void deleteById(Long id) {
        depotMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(Depot depot) {
        if (depot.getId() != null) {
            depotMapper.updateByPrimaryKey(depot);
        } else {
            depotMapper.insert(depot);
        }
    }

    public Depot getById(Long id) {
        return depotMapper.selectByPrimaryKey(id);
    }

    public List<Depot> selectAll() {
        return depotMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = depotMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<Depot> data = depotMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    @Override
    public void deleteall(Long[] ids) {
        if (ids!=null){
            for (Long id : ids) {
                depotMapper.deleteByPrimaryKey(id);
            }
        }
    }
}
